<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数组过滤示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f4f9;
            color: #333;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: white;
            border-radius: 10px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            padding: 30px;
            width: 400px;
            text-align: center;
        }

        h1 {
            color: #4CAF50;
            font-size: 24px;
            margin-bottom: 20px;
        }

        p {
            font-size: 16px;
            margin-bottom: 20px;
        }

        .output {
            background-color: #f9f9f9;
            border: 1px solid #ddd;
            border-radius: 5px;
            padding: 10px;
            font-size: 16px;
            min-height: 50px;
            display: flex;
            justify-content: center;
            align-items: center;
            color: #4CAF50;
            font-weight: bold;
            margin-top: 1px;
        }

        button {
            background-color: #4CAF50;
            color: white;
            padding: 10px 20px;
            border: none;
            border-radius: 5px;
            cursor: pointer;
            font-size: 16px;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #45a049;
        }
    </style>
</head>

<body>

    <div class="container">
        <h1>数组过滤示例</h1>
        <p>点击按钮，过滤掉数组中的无效值</p>

        <button onclick="filterArray()">过滤数组</button>

        <div class="output" id="output">输出结果将在这里显示</div>

    </div>

    <script>
        // 定义 compact 函数，过滤掉数组中的无效值
        const compact = arr => arr.filter(Boolean);

        // 数组示例
        const exampleArray = [0, 'Hello', false, 'World', undefined, '', 'JavaScript', null, 42];

        // 点击按钮时触发的函数
        function filterArray() {
            const filteredArray = compact(exampleArray);
            document.getElementById('output').textContent = `过滤后的数组: [${filteredArray.join(', ')}]`;
        }
    </script>

</body>

</html>